package com.me.study.rabbitmq.consumer;

import com.me.study.rabbitmq.config.GlobalEventInput;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.Message;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.support.ErrorMessage;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * @author light
 * @version 1.0
 * @description: TODO
 * @date 2021/10/18 14:25
 */
@Component
public class MqHandler {

    @StreamListener(GlobalEventInput.TestDirectExchange_INPUT)
    public void listenMessage(@Payload Map<String, Object> map,@Header(AmqpHeaders.CHANNEL) Channel channel
            , @Header(AmqpHeaders.DELIVERY_TAG) Long deliveryTag){
        System.out.println("TestDirectExchange_INPUT 接收到："+map.toString());
        try {
            channel.basicAck(deliveryTag,false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @StreamListener(GlobalEventInput.TopicExchange_ALL_INPUT)
    public void listenTopicAll(@Payload Map<String, Object> map,@Header(AmqpHeaders.CHANNEL) Channel channel
            , @Header(AmqpHeaders.DELIVERY_TAG) Long deliveryTag){
        System.out.println("TopicExchange_ALL_INPUT 接收到："+map.toString());
        try {
            channel.basicAck(deliveryTag,false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @StreamListener(GlobalEventInput.TopicExchange_MAN_INPUT)
    public void listenTopicMan(@Payload Map<String, Object> map,@Header(AmqpHeaders.CHANNEL) Channel channel
            , @Header(AmqpHeaders.DELIVERY_TAG) Long deliveryTag){
        System.out.println("TopicExchange_MAN_INPUT 接收到："+map.toString());
        try {
            channel.basicAck(deliveryTag,false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @StreamListener(GlobalEventInput.FanoutExchange_INPUT)
    public void listenFanout(@Payload Map<String, Object> map,@Header(AmqpHeaders.CHANNEL) Channel channel
            , @Header(AmqpHeaders.DELIVERY_TAG) Long deliveryTag){
        System.out.println("FanoutExchange_INPUT 接收到："+map.toString());
        throw new RuntimeException("23");
        /*try {
            channel.basicReject(deliveryTag,false);
        } catch (IOException e) {
            e.printStackTrace();
        }*/
    }

    @StreamListener(GlobalEventInput.DIRECT_BACKUP_EXCHANGE_INPUT)
    public void listenBackup(@Payload Map<String, Object> map,@Header(AmqpHeaders.CHANNEL) Channel channel
            , @Header(AmqpHeaders.DELIVERY_TAG) Long deliveryTag){
        System.out.println("DIRECT_BACKUP_EXCHANGE_INPUT 接收到："+map.toString());

        try {
            channel.basicAck(deliveryTag,false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
